package com.toune.movie.mvp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.nightonke.boommenu.BoomButtons.BoomButton;
import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenu.OnBoomListener;
import com.tmall.ultraviewpager.UltraViewPager;
import com.tmall.ultraviewpager.transformer.UltraDepthScaleTransformer;
import com.toune.movie.R;
import com.toune.movie.base.Api;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.BaseFragment;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.base.myUtil.NoHttpCallBack;
import com.toune.movie.base.myUtil.RequstUtils;
import com.toune.movie.base.myview.BuilderManager;
import com.toune.movie.mvp.activity.ComicVideoActivity;
import com.toune.movie.mvp.activity.HistoryActivity;
import com.toune.movie.mvp.activity.MovieVideoActivity;
import com.toune.movie.mvp.activity.SearchActivity;
import com.toune.movie.mvp.activity.TvVideoActivity;
import com.toune.movie.mvp.activity.VarietyDetailActivity;
import com.toune.movie.mvp.activity.VarietyVideoActivity;
import com.toune.movie.mvp.adapter.UltraPagerAdapter;
import com.toune.movie.mvp.contract.MovieMainContract;
import com.toune.movie.mvp.presenter.MovieMainPresenter;
import com.toune.movie.vo.VarietyVo;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxDataTool;
import com.vondear.rxtool.RxKeyboardTool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by Administrator on 2018/5/9.
 */

public class MovieMainFragment extends BaseFragment<MovieMainPresenter> implements MovieMainContract.View {

    private static MovieMainFragment fragment;
    @BindView(R.id.ultra_viewpager)
    UltraViewPager ultraViewPager;
    @BindView(R.id.banner_guide_content)
    BGABanner bannerGuideContent;
    @BindView(R.id.bmb2)
    BoomMenuButton bmb2;
    @BindView(R.id.history_iv)
    ImageView historyIv;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_btn)
    Button searchBtn;
    @BindView(R.id.search_fl)
    LinearLayout searchFl;
    private List<VarietyVo> allVideoList = new ArrayList<>();
    private List<VarietyVo> headersVos = new ArrayList<>();

    public static MovieMainFragment newInstance() {
        if (fragment == null) {
            fragment = new MovieMainFragment();
        }
        return fragment;
    }

    @Override // 实例化presenter
    public MovieMainPresenter initPresenter() {
        return new MovieMainPresenter();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void init(Bundle savedInstanceState) {
        MyUtil.showProDialog(getContext());
        mPresenter.getVo(1);
        for (int i = 0; i < 4; i++) {
            bmb2.addBuilder(BuilderManager.getSquareTextOutsideCircleButtonBuilder());
        }

        bmb2.setOnBoomListener(new OnBoomListener() {
            @Override
            public void onClicked(int index, BoomButton boomButton) {
                switch (index) {
                    case 0:
                        RxActivityTool.skipActivity(getContext(), MovieVideoActivity.class);
                        break;
                    case 1:
                        RxActivityTool.skipActivity(getContext(), TvVideoActivity.class);
                        break;
                    case 2:
                        RxActivityTool.skipActivity(getContext(), VarietyVideoActivity.class);
                        break;
                    case 3:
                        RxActivityTool.skipActivity(getContext(), ComicVideoActivity.class);
                        break;
                }
            }

            @Override
            public void onBackgroundClick() {

            }

            @Override
            public void onBoomWillHide() {

            }

            @Override
            public void onBoomDidHide() {

            }

            @Override
            public void onBoomWillShow() {

            }

            @Override
            public void onBoomDidShow() {

            }
        });
    }

    private void requset(String string) {
        MyUtil.showProDialog(getContext());
        if (RxDataTool.isEmpty(string)) {
            return;
        }
        NoHttpCallBack.params.put("wd", string);
        RequstUtils.POST(Api.SearchUrl, false, new NoHttpCallBack<String>(String.class) {
            @Override
            protected void getSucceed(String o) {
                MyUtil.hidProDialog();
                Intent intent = new Intent(getContext(), SearchActivity.class);
                intent.putExtra("html", o);
                startActivity(intent);
            }

            @Override
            protected void getFailed(String json) {

            }
        });
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_movie_main;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    @Override
    public void setVo(List<VarietyVo> videoList) {
        allVideoList = videoList;
        handler.sendEmptyMessage(0);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    updateUltraView();
                    break;
                case 1:
                    updateBGABannerView();
                    break;
            }
        }
    };

    private void updateBGABannerView() {
        /**
         * 设置是否开启自动轮播，需要在 setData 方法之前调用，并且调了该方法后必须再调用一次 setData 方法
         * 例如根据图片当图片数量大于 1 时开启自动轮播，等于 1 时不开启自动轮播
         */
        bannerGuideContent.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(getContext())
                        .load(model)
                        .apply(new RequestOptions().placeholder(R.mipmap.holder).error(R.mipmap.holder).dontAnimate().centerCrop())
                        .into(itemView);
            }
        });
        bannerGuideContent.setAutoPlayInterval(6000);
        String image1 = "http://www.a2yy.com//images/nopic.gif", image2 = "http://www.a2yy.com//images/nopic.gif",
                image3 = "http://www.a2yy.com//images/nopic.gif",
                title1 = "", title2 = "", title3 = "";
        if (headersVos.size() > 1) {
            image1 = headersVos.get(0).getImagSrc();
            title1 = headersVos.get(0).getTitle();
        }
        if (headersVos.size() > 2) {
            image2 = headersVos.get(1).getImagSrc();
            title2 = headersVos.get(1).getTitle();
        }
        if (headersVos.size() > 3) {
            image3 = headersVos.get(2).getImagSrc();
            title3 = headersVos.get(2).getImagSrc();
        }
        bannerGuideContent.setData(Arrays.asList(image1, image2, image3), Arrays.asList(title1, title2, title3));
        bannerGuideContent.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                Intent intent = new Intent(getContext(), VarietyDetailActivity.class);
                intent.putExtra("url", headersVos.get(position).getDetailUrl());
                intent.putExtra("title", headersVos.get(position).getTitle());
                getContext().startActivity(intent);
            }
        });

        MyUtil.hidProDialog();
    }

    private void updateUltraView() {
        ultraViewPager.setScrollMode(UltraViewPager.ScrollMode.HORIZONTAL);
//UltraPagerAdapter 绑定子view到UltraViewPager
        PagerAdapter adapter = new UltraPagerAdapter(allVideoList, true);
        ultraViewPager.setAdapter(adapter);

//内置indicator初始化
//            ultraViewPager.initIndicator();
//设置indicator样式
//            ultraViewPager.getIndicator()
//                    .setOrientation(UltraViewPager.Orientation.HORIZONTAL)
//                    .setFocusColor(R.color.mainColor)
//                    .setNormalColor(R.color.white)
//                    .setRadius((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()));
////设置indicator对齐方式
//            ultraViewPager.getIndicator().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
//构造indicator,绑定到UltraViewPager
//            ultraViewPager.getIndicator().build();

//设定页面循环播放
        ultraViewPager.setInfiniteLoop(true);
//设定页面自动切换  间隔2秒
        ultraViewPager.setAutoScroll(2000);
        ultraViewPager.setMultiScreen(0.6f);
        ultraViewPager.setItemRatio(1.0f);
        ultraViewPager.setRatio(2.0f);
        ultraViewPager.setMaxHeight(800);
        ultraViewPager.setAutoMeasureHeight(true);
//            ultraViewPager.setMultiScreen(0.5f);
//            ultraViewPager.setItemRatio(1.0f);
//            ultraViewPager.setAutoMeasureHeight(true);
        ultraViewPager.setPageTransformer(false, new UltraDepthScaleTransformer());
    }

    @Override
    public void setHeaders(List<VarietyVo> voList) {
        headersVos = voList;
        handler.sendEmptyMessage(1);
    }

    @OnClick({R.id.search_et, R.id.search_btn, R.id.history_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_et:
                RxKeyboardTool.showSoftInput(getContext(), searchEt);
                break;
            case R.id.search_btn:
                break;
            case R.id.history_iv:
                RxActivityTool.skipActivity(getContext(), HistoryActivity.class);
                break;
        }
    }
}
